package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.article.vo.TravelStatisPersistenceVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * Created by LENOVO on 2019/8/12.
 */
@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper travelContentMapper;




    @Override
    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());//qo.getOrderBy()为排序的规则
        return new PageInfo(travelMapper.selectForList(qo));
    }

    @Override
    public Long saveOrUpdate(Travel travel) {


        //设置作者



        //设置摘要
        String content = travel.getTravelContent().getContent();
        if(content.length() >=200){
            travel.setSummary(content.substring(0,200));
        }else {
            travel.setSummary(content);
        }


        //设置最后跟新时间
        travel.setLastUpdateTime(new Date());

        if(travel.getId()==null){
            //设置创建时间
            travel.setCreateTime(new Date());



            travelMapper.insert(travel);
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());//设置id
            travelContentMapper.insert(travelContent);
        }else {
            travelMapper.updateByPrimaryKey(travel);
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());//设置id
            travelContentMapper.updateByPrimaryKey(travelContent);

        }
        //返回id
        return travel.getId();

    }

    @Override
    public List<Travel> list() {

        return travelMapper.selectAll();
    }

    @Override
    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> getTravelTop3(Long destId) {

        return travelMapper.selectViewNumTop3(destId);
    }

    @Override
    public TravelContent getContent(Long id) {
        return travelContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateState(Long id, int state) {
        travelMapper.updateState(id,state);
    }


    //游记的定时跟新
    @Override
    public void updateStatisVO(TravelStatisPersistenceVO vo) {
        travelMapper.updateStatisVO(vo);
    }


}
